<a href='https://github.com/angular/angular.js/edit/v1.4.x/src/ng/directive/attrs.js?message=docs(ngHref)%3A%20describe%20your%20change...#L3' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this Doc</a>



<a href='https://github.com/angular/angular.js/tree/v1.4.7/src/ng/directive/attrs.js#L3' class='view-source pull-right btn btn-primary'>
  <i class="glyphicon glyphicon-zoom-in">&nbsp;</i>View Source
</a>


<header class="api-profile-header">
  <h1 class="api-profile-header-heading">ngHref</h1>
  <ol class="api-profile-header-structure naked-list step-list">
    
    <li>
      - directive in module <a href="api/ng">ng</a>
    </li>
  </ol>
</header>



<div class="api-profile-description">
  <p>Using Angular markup like <code>{{hash}}</code> in an href attribute will
make the link go to the wrong URL if the user clicks it before
Angular has a chance to replace the <code>{{hash}}</code> markup with its
value. Until Angular replaces the markup the link will be broken
and will most likely return a 404 error. The <code>ngHref</code> directive
solves this problem.</p>
<p>The wrong way to write it:</p>
<pre><code class="lang-html">&lt;a href=&quot;http://www.gravatar.com/avatar/{{hash}}&quot;&gt;link1&lt;/a&gt;
</code></pre>
<p>The correct way to write it:</p>
<pre><code class="lang-html">&lt;a ng-href=&quot;http://www.gravatar.com/avatar/{{hash}}&quot;&gt;link1&lt;/a&gt;
</code></pre>

</div>




<div>
  

  
  <h2>Directive Info</h2>
  <ul>
    
    <li>This directive executes at priority level 99.</li>
    
  </ul>

  
  <h2 id="usage">Usage</h2>
  <div class="usage">
  
    <ul>
    <li>as attribute:
        <pre><code>&lt;A&#10;  ng-href=&quot;template&quot;&gt;&#10;...&#10;&lt;/A&gt;</code></pre>
      </li>
    
  </div>
  
<section class="api-section">
  <h3>Arguments</h3>

<table class="variables-matrix input-arguments">
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    
    <tr>
      <td>
        ngHref
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-template">template</a>
      </td>
      <td>
        <p>any string which can contain <code>{{}}</code> markup.</p>

        
      </td>
    </tr>
    
  </tbody>
</table>

</section>
  


  
  <h2 id="example">Example</h2><p>This example shows various combinations of <code>href</code>, <code>ng-href</code> and <code>ng-click</code> attributes
in links and their different behaviors:
    

<div>
  <a ng-click="openPlunkr('examples/example-example53', $event)" class="btn pull-right">
    <i class="glyphicon glyphicon-edit">&nbsp;</i>
    Edit in Plunker</a>

  <div class="runnable-example"
      path="examples/example-example53">

  
    <div class="runnable-example-file" 
      name="index.html"
      language="html"
      type="html">
      <pre><code>&lt;input ng-model=&quot;value&quot; /&gt;&lt;br /&gt;&#10;&lt;a id=&quot;link-1&quot; href ng-click=&quot;value = 1&quot;&gt;link 1&lt;/a&gt; (link, don&#39;t reload)&lt;br /&gt;&#10;&lt;a id=&quot;link-2&quot; href=&quot;&quot; ng-click=&quot;value = 2&quot;&gt;link 2&lt;/a&gt; (link, don&#39;t reload)&lt;br /&gt;&#10;&lt;a id=&quot;link-3&quot; ng-href=&quot;/{{&#39;123&#39;}}&quot;&gt;link 3&lt;/a&gt; (link, reload!)&lt;br /&gt;&#10;&lt;a id=&quot;link-4&quot; href=&quot;&quot; name=&quot;xx&quot; ng-click=&quot;value = 4&quot;&gt;anchor&lt;/a&gt; (link, don&#39;t reload)&lt;br /&gt;&#10;&lt;a id=&quot;link-5&quot; name=&quot;xxx&quot; ng-click=&quot;value = 5&quot;&gt;anchor&lt;/a&gt; (no link)&lt;br /&gt;&#10;&lt;a id=&quot;link-6&quot; ng-href=&quot;{{value}}&quot;&gt;link&lt;/a&gt; (link, change location)</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="protractor.js"
      type="protractor"
      language="js">
      <pre><code>it(&#39;should execute ng-click but not reload when href without value&#39;, function() {&#10;  element(by.id(&#39;link-1&#39;)).click();&#10;  expect(element(by.model(&#39;value&#39;)).getAttribute(&#39;value&#39;)).toEqual(&#39;1&#39;);&#10;  expect(element(by.id(&#39;link-1&#39;)).getAttribute(&#39;href&#39;)).toBe(&#39;&#39;);&#10;});&#10;&#10;it(&#39;should execute ng-click but not reload when href empty string&#39;, function() {&#10;  element(by.id(&#39;link-2&#39;)).click();&#10;  expect(element(by.model(&#39;value&#39;)).getAttribute(&#39;value&#39;)).toEqual(&#39;2&#39;);&#10;  expect(element(by.id(&#39;link-2&#39;)).getAttribute(&#39;href&#39;)).toBe(&#39;&#39;);&#10;});&#10;&#10;it(&#39;should execute ng-click and change url when ng-href specified&#39;, function() {&#10;  expect(element(by.id(&#39;link-3&#39;)).getAttribute(&#39;href&#39;)).toMatch(/\/123$/);&#10;&#10;  element(by.id(&#39;link-3&#39;)).click();&#10;&#10;  // At this point, we navigate away from an Angular page, so we need&#10;  // to use browser.driver to get the base webdriver.&#10;&#10;  browser.wait(function() {&#10;    return browser.driver.getCurrentUrl().then(function(url) {&#10;      return url.match(/\/123$/);&#10;    });&#10;  }, 5000, &#39;page should navigate to /123&#39;);&#10;});&#10;&#10;it(&#39;should execute ng-click but not reload when href empty string and name specified&#39;, function() {&#10;  element(by.id(&#39;link-4&#39;)).click();&#10;  expect(element(by.model(&#39;value&#39;)).getAttribute(&#39;value&#39;)).toEqual(&#39;4&#39;);&#10;  expect(element(by.id(&#39;link-4&#39;)).getAttribute(&#39;href&#39;)).toBe(&#39;&#39;);&#10;});&#10;&#10;it(&#39;should execute ng-click but not reload when no href but name specified&#39;, function() {&#10;  element(by.id(&#39;link-5&#39;)).click();&#10;  expect(element(by.model(&#39;value&#39;)).getAttribute(&#39;value&#39;)).toEqual(&#39;5&#39;);&#10;  expect(element(by.id(&#39;link-5&#39;)).getAttribute(&#39;href&#39;)).toBe(null);&#10;});&#10;&#10;it(&#39;should only change url when only ng-href&#39;, function() {&#10;  element(by.model(&#39;value&#39;)).clear();&#10;  element(by.model(&#39;value&#39;)).sendKeys(&#39;6&#39;);&#10;  expect(element(by.id(&#39;link-6&#39;)).getAttribute(&#39;href&#39;)).toMatch(/\/6$/);&#10;&#10;  element(by.id(&#39;link-6&#39;)).click();&#10;&#10;  // At this point, we navigate away from an Angular page, so we need&#10;  // to use browser.driver to get the base webdriver.&#10;  browser.wait(function() {&#10;    return browser.driver.getCurrentUrl().then(function(url) {&#10;      return url.match(/\/6$/);&#10;    });&#10;  }, 5000, &#39;page should navigate to /6&#39;);&#10;});</code></pre>
    </div>
  

    <iframe class="runnable-example-frame" src="examples/example-example53/index.html" name="example-example53"></iframe>
  </div>
</div>


</p>

</div>


